﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Dict.Contracts
{
    public partial class frmAddContract : Form
    {
        public BindingSource bs;
        private string strFormEditType;

        public frmAddContract(string strType)
        {
            strFormEditType = strType;

            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAddContract_Load(object sender, EventArgs e)
        {
            if (strFormEditType == "add")
            {
                btnChange.Visible = false;
                btnSave.Visible = true;
            }
            else
            {
                btnChange.Visible = true;
                btnSave.Visible = false;
            }

            if (bs!=null)
            {
                //this.Text=
                //this.comboBox1.DataBindings.Add("Text", bs, "cntStatus");
                this.txtContractNumber.DataBindings.Add("Text", bs, "cntNum");
                //this.cbxPayType.DataBindings.Add("Text", bs, "PayType");
                this.txtAcc.DataBindings.Add("Text", bs, "Acc");
                this.txtSumm.DataBindings.Add("Text", bs, "cntVn");
                this.cbxStatus.DataBindings.Add("Text", bs, "cntStatus");
                this.dtpStart.DataBindings.Add("Text", bs, "cntD1");
                this.dtpEnd.DataBindings.Add("Text", bs, "cntD2");
                this.dtpStop.DataBindings.Add("Text", bs, "cntD3");
                this.mxeText.DataBindings.Add("Text", bs, "cntText");
                this.mxeComment.DataBindings.Add("Text", bs, "cntNote");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlCommand cmdSQL=new SqlCommand();
            System.Data.SqlClient.SqlConnection cnSQL=new SqlConnection(globals.APPCONNSTRING);

            int intStatus=0;
            //MessageBox.Show(cbxStatus.SelectedItem.ToString());
            //MessageBox.Show(cbxStatus.SelectedValue.ToString());
            //MessageBox.Show(cbxStatus.SelectedText);

            switch (cbxStatus.SelectedItem.ToString())
            {
                case "Пока не действителен":
                    intStatus = 0;
                    break;
                case "Действителен":
                    intStatus = 1;
                    break;
                case "Приостановлен":
                    intStatus = 2;
                    break;
                default:
                    intStatus = 0;
                    break;
            }
            //MessageBox.Show(intStatus.ToString());

            cmdSQL.Connection = cnSQL;
            cmdSQL.CommandText = "tsp_Contracts";
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 0;
            cmdSQL.Parameters.Add("@PayType", SqlDbType.SmallInt).Value = 1;
            cmdSQL.Parameters.Add("@Acc", SqlDbType.NVarChar, 50).Value = txtAcc.Text.Trim();
            cmdSQL.Parameters.Add("@cntNum", SqlDbType.NVarChar, 50).Value = txtContractNumber.Text.Trim();
            cmdSQL.Parameters.Add("@cntVn", SqlDbType.Money).Value = txtSumm.Text.Trim();
            cmdSQL.Parameters.Add("@cntD1", SqlDbType.SmallDateTime).Value = dtpStart.Value;
            cmdSQL.Parameters.Add("@cntD2", SqlDbType.SmallDateTime).Value = dtpEnd.Value;
            cmdSQL.Parameters.Add("@cntD3", SqlDbType.SmallDateTime).Value = dtpStop.Value;
            cmdSQL.Parameters.Add("@cntText", SqlDbType.NText).Value = mxeText.Text;
            cmdSQL.Parameters.Add("@cntNote", SqlDbType.NVarChar, 250).Value = mxeComment.Text;
            cmdSQL.Parameters.Add("@cntStatus", SqlDbType.TinyInt).Value = intStatus;

            try
            {
                cnSQL.Open();
                cmdSQL.ExecuteNonQuery();
                cnSQL.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка записи данных! \n" + ex.Message, "TIMS");

                //throw Exception;
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();
            System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection(globals.APPCONNSTRING);

            cmdSQL.Connection = cnSQL;
            cmdSQL.CommandText = "tsp_Contracts";
            cmdSQL.CommandType = CommandType.StoredProcedure;
            //cmdSQL.Parameters.Add("@cntID", SqlDbType.Int).Value = bs.Current[0].ToString();

            int intStatus = 0;
            //MessageBox.Show(cbxStatus.SelectedItem.ToString());
            //MessageBox.Show(cbxStatus.SelectedValue.ToString());
            //MessageBox.Show(cbxStatus.SelectedText);

            switch (cbxStatus.SelectedItem.ToString())
            {
                case "Пока не действителен":
                    intStatus = 0;
                    break;
                case "Действителен":
                    intStatus = 1;
                    break;
                case "Приостановлен":
                    intStatus = 2;
                    break;
                default:
                    intStatus = 0;
                    break;
            }

            //DataRowView drv;
            //dr=(datarowvi)
            DataRowView drv = (DataRowView) bs.Current;
            //MessageBox.Show(drv["cntStatus"].ToString());

            //MessageBox.Show(drv[0].ToString());
            cmdSQL.Parameters.Add("@cntID", SqlDbType.Int).Value = (int) drv[0];
            cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 1;
            cmdSQL.Parameters.Add("@PayType", SqlDbType.SmallInt).Value = 1;
            cmdSQL.Parameters.Add("@Acc", SqlDbType.NVarChar, 10).Value = txtAcc.Text.Trim();
            cmdSQL.Parameters.Add("@cntNum", SqlDbType.NVarChar, 10).Value = txtContractNumber.Text.Trim();
            cmdSQL.Parameters.Add("@cntVn", SqlDbType.Money).Value = txtSumm.Text.Trim();
            cmdSQL.Parameters.Add("@cntD1", SqlDbType.SmallDateTime).Value = dtpStart.Value;
            cmdSQL.Parameters.Add("@cntD2", SqlDbType.SmallDateTime).Value = dtpEnd.Value;
            cmdSQL.Parameters.Add("@cntD3", SqlDbType.SmallDateTime).Value = dtpStop.Value;
            cmdSQL.Parameters.Add("@cntText", SqlDbType.NText).Value = mxeText.Text;
            cmdSQL.Parameters.Add("@cntNote", SqlDbType.NVarChar, 250).Value = mxeComment.Text;
            cmdSQL.Parameters.Add("@cntStatus", SqlDbType.TinyInt).Value = intStatus;

            try
            {
                cnSQL.Open();
                cmdSQL.ExecuteNonQuery();
                cnSQL.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка записи данных! \n" + ex.Message, "TIMS");

                //throw Exception;
            }
        }
    }
}
